package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.config.VideoCategory;
import com.bsb.base.common.util.IdGen;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbCategory;
import com.bsb.base.model.BsbVideo;
import com.bsb.base.model.BsbVideoExample;
import com.bsb.base.model.BsbVideoExample.Criteria;
import com.bsb.biz.service.BsbCategoryService;
import com.bsb.biz.service.BsbVideoService;

@Controller
@RequestMapping("/bsbVideo")
public class BsbVideoController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbVideo/list.do";
	
	@Resource
	private BsbVideoService bsbVideoService;
	@Resource
	private BsbCategoryService bsbCategoryService;
	
	@RequiresPermissions("bsbVideo:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,String title,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbVideoExample bsbVideoExample = new BsbVideoExample();
		Criteria criteria = bsbVideoExample.createCriteria();
		if(StringUtils.isNotEmpty(title)){
			criteria.andTitleLike("%"+title+"%");
		}
		int totalCount = bsbVideoService.countBsbVideo(bsbVideoExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbVideoExample.setPageIndex((pageNum - 1) * pageSize);
		bsbVideoExample.setPageSize(pageSize);
		bsbVideoExample.setOrderByClause("createDate DESC");
		List<BsbVideo> bsbVideos = bsbVideoService.getPageBsbVideo(bsbVideoExample);
		for (BsbVideo bsbVideo : bsbVideos) {
			String trade = bsbCategoryService.getCategoryName(bsbVideo.getTrade());
			String tax = bsbCategoryService.getCategoryName(bsbVideo.getTax());
			String topic= bsbCategoryService.getCategoryName(bsbVideo.getTopic());
			bsbVideo.setTrade(trade);
			bsbVideo.setTax(tax);
			bsbVideo.setTopic(topic);
			bsbVideo.setType(VideoCategory.getName(bsbVideo.getType()));
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbVideos", bsbVideos);
		model.addAttribute("title", title);
		return "/BsbVideo/list";
	}
	
	@RequiresPermissions("bsbVideo:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		List<VideoCategory> list = VideoCategory.getList();
		List<BsbCategory> trades = bsbCategoryService.childList("1");//行业
		List<BsbCategory> taxs = bsbCategoryService.childList("2");//税种
		List<BsbCategory> topics = bsbCategoryService.childList("3");//专题
		model.addAttribute("trades", trades);
		model.addAttribute("taxs", taxs);
		model.addAttribute("topics", topics);
		model.addAttribute("videoCategory", list);
		return "/BsbVideo/add";
	}

	@RequiresPermissions("bsbVideo:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model,RedirectAttributes redirectAttributes, @ModelAttribute("bsbVideo") BsbVideo bsbVideo) {
		bsbVideo.setId(IdGen.uuid());
		bsbVideo.setCreateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbVideo.setCreateDate(new Date());
		bsbVideoService.addBsbVideo(bsbVideo);
		redirectAttributes.addFlashAttribute("msg", "新增成功");
		return redirect;
	}
	
	@RequiresPermissions("bsbVideo:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("id") String id) {
		bsbVideoService.delBsbVideoById(id);
		return redirect;
	}
	@RequiresPermissions("bsbVideo:delete")
	@RequestMapping(value = "/{id}/hide", method = RequestMethod.GET)
	public String hide(Model model,RedirectAttributes redirectAttributes,
			@PathVariable("id") String id) {
		BsbVideo bsbVideo = new BsbVideo();
		bsbVideo.setId(id);
		bsbVideo.setDelFlag(Global.HIDE);
		bsbVideo.setUpdateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbVideo.setUpdateDate(new Date());
		bsbVideoService.updateBsbVideo(bsbVideo);
		redirectAttributes.addFlashAttribute("msg", "隐藏成功");
		return redirect;
	}
	@RequiresPermissions("bsbVideo:delete")
	@RequestMapping(value = "/{id}/show", method = RequestMethod.GET)
	public String show(Model model,RedirectAttributes redirectAttributes,
			@PathVariable("id") String id) {
		BsbVideo bsbVideo = new BsbVideo();
		bsbVideo.setId(id);
		bsbVideo.setDelFlag(Global.SHOW);
		bsbVideo.setUpdateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbVideo.setUpdateDate(new Date());
		bsbVideoService.updateBsbVideo(bsbVideo);
		redirectAttributes.addFlashAttribute("msg", "显示成功");
		return redirect;
	}
	
	@RequiresPermissions("bsbVideo:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toEdit(Model model,HttpServletRequest request,
			@PathVariable("id") String id) {
		BsbVideo bsbVideo = bsbVideoService.getBsbVideoById(id);
		List<VideoCategory> list = VideoCategory.getList();
		List<BsbCategory> trades = bsbCategoryService.childList("1");//行业
		List<BsbCategory> taxs = bsbCategoryService.childList("2");//税种
		List<BsbCategory> topics = bsbCategoryService.childList("3");//专题
		model.addAttribute("trades", trades);
		model.addAttribute("taxs", taxs);
		model.addAttribute("topics", topics);
		model.addAttribute("videoCategory", list);
		model.addAttribute("bsbVideo", bsbVideo);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		return "/BsbVideo/edit";
	}

	@RequiresPermissions("bsbVideo:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model,
			@RequestParam(value = "queryString", required = false) String queryString,
			RedirectAttributes redirectAttributes, @ModelAttribute("bsbVideo") BsbVideo bsbVideo) {
		bsbVideo.setUpdateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbVideo.setUpdateDate(new Date());
		bsbVideoService.updateBsbVideo(bsbVideo);
		redirectAttributes.addFlashAttribute("msg", "修改成功");
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbVideo:view")
	@RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("id") String id) {
		BsbVideo bsbVideo = bsbVideoService.getBsbVideoById(id);
		String trade = bsbCategoryService.getCategoryName(bsbVideo.getTrade());
		String tax = bsbCategoryService.getCategoryName(bsbVideo.getTax());
		String topic= bsbCategoryService.getCategoryName(bsbVideo.getTopic());
		bsbVideo.setTrade(trade);
		bsbVideo.setTax(tax);
		bsbVideo.setTopic(topic);
		bsbVideo.setType(VideoCategory.getName(bsbVideo.getType()));
		model.addAttribute("bsbVideo", bsbVideo);
		return "/BsbVideo/view";
	}
}
